
// <copyright file="AsyncResult.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The async result.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Threading
{
    using System;
    using System.Threading;

    /// <summary>
    /// The async result.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class AsyncResult<T> : AbstractDisposable, IAsyncResult
    {
        #region Fields

        /// <summary>
        /// The wait handle.
        /// </summary>
        private readonly ManualResetEvent _waitHandle = new ManualResetEvent(false);

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncResult{T}"/> class.
        /// </summary>
        internal AsyncResult()
        {
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the async state.
        /// </summary>
        public object AsyncState
        {
            get
            {
                return this.TypedAsyncState;
            }
        }

        /// <summary>
        /// Gets the async wait handle.
        /// </summary>
        public WaitHandle AsyncWaitHandle
        {
            get
            {
                return this._waitHandle;
            }
        }

        /// <summary>
        /// Gets a value indicating whether completed synchronously.
        /// </summary>
        public bool CompletedSynchronously
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the exception.
        /// </summary>
        public Exception Exception { get; private set; }

        /// <summary>
        /// Gets a value indicating whether is completed.
        /// </summary>
        public bool IsCompleted { get; private set; }

        /// <summary>
        /// Gets the typed async state.
        /// </summary>
        public T TypedAsyncState { get; internal set; }

        #endregion Public Properties

        #region Methods

        /// <summary>
        /// The set completed.
        /// </summary>
        /// <param name="returnedValue">
        /// The returned value.
        /// </param>
        internal void SetCompleted(T returnedValue)
        {
            this.IsCompleted = true;
            this.TypedAsyncState = returnedValue;
            this._waitHandle.Set();
        }

        /// <summary>
        /// The set completed with exception.
        /// </summary>
        /// <param name="ex">
        /// The ex.
        /// </param>
        internal void SetCompletedWithException(Exception ex)
        {
            this.IsCompleted = true;
            this.Exception = ex;
            this._waitHandle.Set();
        }

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            this.AsyncWaitHandle.Dispose();
        }

        #endregion Methods
    }
}